I would like to invoke a program /usr/bin/xyz from within an Icon program and use pipes to communicate with it. I would send the command strings to xyz with a write(), and then read the results with a read(). It is important to understand that both the read() and the write() are called from within a single program.
I tried some code that looked something like this (assume xyz is already running):
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...
# to_xyz is the write pipe to /usr/bin/xyz
# fr_xyz is the read pipe from /usr/bin/xyz
# Read lines from standard input, send them to xyz.
# Then read xyz's response and print it to standard output.
while line_in := read() do {
write(to_xyz, line_in)
# Make sure buffer is flushed, then:
while line_out := read(fr_xyz) do
write(line_out)
}
...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The problem is, Icon's pipe facility won't permit setting up the pipes. The
syntax is
fr_xyz := open("/usr/bin/xyz", "pr") # read pipe
to_xyz := open("/usr/bin/xyz", "pw") # write pipe
This implies that each open() can open only a single, unidirectional pipe to a fresh instance of /usr/bin/xyz. And that is true. When I did this, I got two separate instances of xyz, each running a separate pipe.
So the question is, is there any way to accomplish this entirely within Icon? I'm sure I could with C, or with a bash script.